<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/8/21
 * Time: 14:13
 */

namespace ticket\modules\backend\controllers;

use ticket\modules\backend\models\Application;
use ticket\modules\backend\models\KlyChannel;
use ticket\modules\backend\models\OrderInfo;
use ticket\modules\backend\models\TicketAddionalAttr;
use ticket\modules\backend\models\TicketAdditional;
use ticket\modules\backend\models\TicketCategorys;
use ticket\modules\backend\models\TicketCode;
use ticket\modules\backend\models\TicketInfo;
use ticket\modules\backend\models\TicketSearch;
use ticket\modules\backend\models\TicketType;
use Yii;
use ticket\libs\Tools;
use yii\base\Exception;

/*** 门票基础信息
 * Class TicketController
 * @package ticket\modules\backend\controllers
 */
class TicketController extends BasicController
{
    /*** 门票列表
     * @return mixed
     */
    public function actionTicketList()
    {
        $get = Yii::$app->request->get();
        $page = isset($get['page'])?$get['page']:1;
        $pageSize = isset($get['size'])?$get['size']:10;
        $serarchModel = (new TicketSearch())->search($get)->andFilterWhere(['tk_ticket_info.app_id' => $this->appId]);
        $count = $serarchModel->count();
        $ticketList = $serarchModel->offset($pageSize * ($page - 1))->joinWith(['type'])->orderBy(['sort' => SORT_DESC,'tid' => SORT_DESC])->limit($pageSize)->asArray()->all();
        if (!empty($ticketList)){
            foreach ($ticketList as $key => $val) {
                if (!empty($val['type'])){
                    $ticketList[$key]['type_id'] = $val['type']['type_name'];
                }
                unset($ticketList[$key]['type']);
            }
        }
        return Tools::showRes(0,'success',[
            'ticketList' => $ticketList,
            'page' => $page,
            'size' => $pageSize,
            'count' => $count,
        ]);

    }

    /*** 添加门票信息接口
     * @return false|string
     */
    public function actionAddTicket()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $transaction = Yii::$app->db->beginTransaction();
            $app_id = $this->appId;
            try{
                $ticketModel = new TicketInfo();

                if (!$tid = $ticketModel->addTicketInfo($app_id,$post)){
                    if ($ticketModel->hasErrors()){
                        throw new Exception(json_encode($ticketModel->getErrors()),10044);
                    }else{
                        throw new Exception('添加门票信息失败',10044);
                    }
                }

                $additionalModel = new TicketAdditional();

                if (!$nid = $additionalModel->additionalInfo($tid,$post)){
                    if ($additionalModel->hasErrors()){
                        throw new Exception(json_encode($additionalModel->getErrors()),10045);
                    }else{
                        throw new Exception('添加门票信息失败',10045);
                    }
                }
                $transaction->commit();
                return Tools::showRes(0,'success');
            }catch (Exception $e){
                $transaction->rollBack();
                return Tools::showRes($e->getCode(),$e->getMessage());
            }
        }
        
    }

    /**** 提交修改门票信息
     * @return false|string
     */
    public function actionModTicketInfo()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $app_id = $this->appId;
            $transaction = Yii::$app->db->beginTransaction();
            try{
                $tid = isset($post['ticketInfo']['tid'])?$post['ticketInfo']['tid']:'';
                if (empty($tid)){
                    throw new Exception('参数有误','99');
                }
                $ticketModel = new TicketInfo();
                // 门票信息
                if (!$ticketModel->modTicketInfo($tid,$app_id,$post)){
                    if ($ticketModel->hasErrors()){
                        throw new Exception(json_encode($ticketModel->getErrors()),10044);
                    }else{
                        throw new Exception('添加门票信息失败',10044);
                    }
                }

                $additionalModel = new TicketAdditional();
                // 门票信息 门票附加信息
                if (!$additionalModel->moditionalInfo($tid,$post)){
                    if ($additionalModel->hasErrors()){
                        throw new Exception(json_encode($additionalModel->getErrors()),10045);
                    }else{
                        throw new Exception('添加门票信息失败',10045);
                    }
                }

                $additionalAttrModel = new TicketAddionalAttr();
                // 门票自定义信息
                if (!$additionalAttrModel->addInfo($tid,$post)){
                    if  ($additionalAttrModel->hasErrors()){
                        throw new Exception(json_encode($additionalAttrModel->getErrors()),10046);
                    }else{
                        throw new Exception('添加门票信息失败',10046);
                    }
                }


                $transaction->commit();
                return Tools::showRes(0,'success');
            }catch (Exception $e){
                $transaction->rollBack();
                return Tools::showRes($e->getCode(),$e->getMessage());
            }
        }
    }

    /*** 获取单个门票信息接口
     * @return false|string
     * @throws \yii\db\Exception
     */
    public function actionGetTicketInfo()
    {
        $get = Yii::$app->request->get();
        $tid = isset($get['tid'])?$get['tid']:'';
        if  (empty($tid)){
            return Tools::showRes(99,'参数有误');
        }
        $ticketModel = new TicketInfo();
        $ticketAttrModel = new TicketAddionalAttr();
        $ticketInfo = $ticketModel->getTicketInfo($tid);
        $ticketAttrInfo = $ticketAttrModel->getTicketAttrInfo($tid);
        if (!$ticketInfo){
            $ticketInfo = [];
        }
        return Tools::showRes(0,'success',[
            'ticketInfo' => $ticketInfo,
            'ticketAttrInfo' => $ticketAttrInfo,
        ]);

    }


    /*** 获取门票分类信息
     * @return false|string
     */
    public function actionGetTicketType()
    {
        $aid = $this->appId;
        $categoryList = TicketCategorys::getTreeList(0,$aid);
        return Tools::showRes(0,'success',[
            'categoryList' => $categoryList,
        ]);

    }

    /**** 获取应用列表
     * @return false|string
     */
    public function actionGetApplication()
    {
        $app_id = $this->appId;
        $appLicationModel = new Application();
        $applicationList = $appLicationModel->getApplicationList($app_id);
        if (empty($applicationList)){
            $applicationList = [];
        }
        return Tools::showRes(0,'success',[
            'applicationList' => $applicationList,
        ]);
        
    }


    /*** 获取票码列表
     * @return false|string
     */
    public function actionTicketCodeList()
    {
        $codeModel = new TicketCode();
        $data = $codeModel->getList($this->appId);
        return Tools::showRes(0,'success',$data);
    }

    /*** 获取优惠券类型
     * @return false|string
     */
    public function actionGetChannel()
    {
        $app_id = $this->appId;
        $list = [];
        if ($app_id == '1'){
            $klyChannelModel = new KlyChannel();
            $list = $klyChannelModel->getList();
        }
        return Tools::showRes(0,'success',$list);
    }


    /**** 删除门票数据
     * @return false|string
     * @throws \Throwable
     */
    public function actionDelTicketInfo()
    {

        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $transacetion = Yii::$app->db->beginTransaction();
            try{
                $tid = isset($post['id'])?$post['id']:'';
                if (empty($tid)){
                    throw new Exception('参数有误',99);
                }
                $ticketModel = new TicketInfo();
                $orderModel = new OrderInfo();
                $orderInfo = $orderModel->getTicketInfo($tid);
                if ($orderInfo){
                    throw new Exception('该门票下有订单未使用',99);
                }
                $res = $ticketModel->delTicketInfo($tid);
                if (!$res){
                    throw new Exception('删除失败',10044);
                }
                $additionalModel = new TicketAdditional();
                $res = $additionalModel->delTicketAdditional($tid);
                if (!$res){
                    throw new Exception('删除失败',10045);
                }
                $additionalAttrModel = new TicketAddionalAttr();

                $res = $additionalAttrModel->delTicketAdditionalAttr($tid);
//                if (!$res){
//                    throw new Exception('删除失败',10046);
//                }
                $transacetion->commit();
                return Tools::showRes(0,'success');
            }catch (Exception $e){
                $transacetion->rollBack();
                return Tools::showRes($e->getCode(),$e->getMessage());
            }
        }





    }


}